﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class CableListFilterForm : System.Windows.Forms.Form
	{
		public CableListFilterForm(List<CableListField> fields)
		{
			this.InitializeComponent();
			this.m_FieldList = fields;
		}

		public List<FilterItem> FilterItems
		{
			get
			{
				return this.m_FilterItems;
			}
		}

		private void CableListFilterForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitRelationCombox();
			this.InitCompareCombox();
			this.InitFieldCombox();
			this.InitDataGridView();
			this.ReadFormDataFromXml();
			this.dataGridView.Controls.Add(this.cmbRelation);
			this.dataGridView.Controls.Add(this.cmbField);
			this.dataGridView.Controls.Add(this.cmbCompare);
		}

		private void InitRelationCombox()
		{
			this.cmbRelation.Items.Add("与");
			this.cmbRelation.Items.Add("或");
		}

		private void InitCompareCombox()
		{
			this.cmbCompare.Items.Add("等于");
			this.cmbCompare.Items.Add("包含");
		}

		private void InitFieldCombox()
		{
			this.cmbField.Items.Clear();
			foreach (CableListField item in this.m_FieldList)
			{
				this.cmbField.Items.Add(item);
			}
			this.cmbField.Items.Add("空");
		}

		private void InitDataGridView()
		{
			for (int i = 0; i < 6; i++)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				int index = this.dataGridView.Rows.Add(dataGridViewRow);
				dataGridViewRow = this.dataGridView.Rows[index];
				dataGridViewRow.Cells[0].Value = string.Empty;
				dataGridViewRow.Cells[1].Value = string.Empty;
				dataGridViewRow.Cells[2].Value = string.Empty;
				dataGridViewRow.Cells[3].Value = string.Empty;
			}
		}

		private void ReadFormDataFromXml()
		{
			List<XmlElement> elements = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData").GetElements("CableListFilter");
			int num = 0;
			foreach (XmlElement node in elements)
			{
				if (num >= this.dataGridView.Rows.Count)
				{
					break;
				}
				string attribute = YArchitech.MEP.Electric.Common.XmlHelper.GetAttribute(node, "Field");
				if (this.GetField(attribute) != null)
				{
					int value = Convert.ToInt32(YArchitech.MEP.Electric.Common.XmlHelper.GetAttribute(node, "Relation"));
					int value2 = Convert.ToInt32(YArchitech.MEP.Electric.Common.XmlHelper.GetAttribute(node, "Compare"));
					string attribute2 = YArchitech.MEP.Electric.Common.XmlHelper.GetAttribute(node, "Value");
					DataGridViewRow dataGridViewRow = this.dataGridView.Rows[num++];
					dataGridViewRow.Cells[0].Value = this.RelationEnumToText((RelationOperator)value);
					dataGridViewRow.Cells[1].Value = attribute;
					dataGridViewRow.Cells[2].Value = this.CompareEnumToText((CompareOperator)value2);
					dataGridViewRow.Cells[3].Value = attribute2;
				}
			}
			this.dataGridView.Rows[0].Cells[0].Value = string.Empty;
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(CableLayCommon.GetUserDataFile(), "ElectricData");
			xmlHelper.SelectNode("CableListFilter");
			XmlElement xmlElement = (xmlHelper.XmlCurNode != null) ? xmlHelper.XmlCurNode : xmlHelper.AddElement("CableListFilter");
			if (xmlElement == null)
			{
				return;
			}
			xmlElement.RemoveAll();
			foreach (FilterItem filterItem in this.m_FilterItems)
			{
				XmlElement node = YArchitech.MEP.Electric.Common.XmlHelper.AddElement(xmlElement, "Item");
				YArchitech.MEP.Electric.Common.XmlHelper.SetAttribute(node, "Field", filterItem.Field.Name);
				YArchitech.MEP.Electric.Common.XmlHelper.SetAttribute(node, "Relation", Convert.ToString((int)filterItem.Relation));
				YArchitech.MEP.Electric.Common.XmlHelper.SetAttribute(node, "Compare", Convert.ToString((int)filterItem.Compare));
				YArchitech.MEP.Electric.Common.XmlHelper.SetAttribute(node, "Value", filterItem.Value);
			}
			xmlHelper.SaveXml("");
		}

		private CableListField GetField(string fieldName)
		{
			if (string.IsNullOrWhiteSpace(fieldName))
			{
				return null;
			}
			if (!this.m_FieldList.Exists((CableListField item) => item.Name == fieldName))
			{
				return null;
			}
			return this.m_FieldList.Find((CableListField item) => item.Name == fieldName);
		}

		private void SelectRelationCombox(string text)
		{
			if (string.IsNullOrWhiteSpace(text))
			{
				this.cmbRelation.SelectedIndex = -1;
				return;
			}
			this.cmbRelation.SelectedItem = text;
		}

		private void SelectFieldCombox(string text)
		{
			if (string.IsNullOrWhiteSpace(text))
			{
				this.cmbField.SelectedIndex = -1;
				return;
			}
			int num = this.FindFieldItem(text);
			if (-1 != num)
			{
				this.cmbField.SelectedIndex = num;
			}
		}

		private void SelectCompareCombox(string text)
		{
			if (string.IsNullOrWhiteSpace(text))
			{
				this.cmbCompare.SelectedIndex = -1;
				return;
			}
			this.cmbCompare.SelectedItem = text;
		}

		private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex == 0 && e.ColumnIndex == 0)
			{
				return;
			}
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count)
			{
				return;
			}
			if (e.ColumnIndex < 0 || e.ColumnIndex >= this.dataGridView.Columns.Count)
			{
				return;
			}
			this.cmbRelation.Visible = false;
			this.cmbCompare.Visible = false;
			this.cmbField.Visible = false;
			this.m_rowIndex = e.RowIndex;
			this.m_columnIndex = e.ColumnIndex;
			Rectangle cellDisplayRectangle = this.dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
			if (e.ColumnIndex == 0)
			{
				this.cmbRelation.Left = cellDisplayRectangle.Left;
				this.cmbRelation.Top = cellDisplayRectangle.Top;
				this.cmbRelation.Width = cellDisplayRectangle.Width;
				this.cmbRelation.Height = cellDisplayRectangle.Height;
				string text = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				this.SelectRelationCombox(text);
				this.cmbRelation.Visible = true;
				return;
			}
			if (1 == e.ColumnIndex)
			{
				this.cmbField.Left = cellDisplayRectangle.Left;
				this.cmbField.Top = cellDisplayRectangle.Top;
				this.cmbField.Width = cellDisplayRectangle.Width;
				this.cmbField.Height = cellDisplayRectangle.Height;
				string text2 = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				this.SelectFieldCombox(text2);
				this.cmbField.Visible = true;
				return;
			}
			if (2 == e.ColumnIndex)
			{
				this.cmbCompare.Left = cellDisplayRectangle.Left;
				this.cmbCompare.Top = cellDisplayRectangle.Top;
				this.cmbCompare.Width = cellDisplayRectangle.Width;
				this.cmbCompare.Height = cellDisplayRectangle.Height;
				string text3 = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
				this.SelectCompareCombox(text3);
				this.cmbCompare.Visible = true;
			}
		}

		private void cmbRelation_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (-1 == this.cmbRelation.SelectedIndex)
			{
				return;
			}
			this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = this.cmbRelation.SelectedItem.ToString();
		}

		private void cmbField_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (-1 == this.cmbField.SelectedIndex)
			{
				return;
			}
			string text = this.cmbField.Text;
			if ("空" == text)
			{
				this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = string.Empty;
				this.dataGridView.Rows[this.m_rowIndex].Cells[0].Value = string.Empty;
				this.dataGridView.Rows[this.m_rowIndex].Cells[2].Value = string.Empty;
				this.dataGridView.Rows[this.m_rowIndex].Cells[3].Value = string.Empty;
				return;
			}
			this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = text;
			if (string.IsNullOrWhiteSpace(this.dataGridView.Rows[this.m_rowIndex].Cells[0].Value.ToString()) && this.m_rowIndex != 0)
			{
				this.dataGridView.Rows[this.m_rowIndex].Cells[0].Value = this.RelationEnumToText(RelationOperator.And);
			}
			if (string.IsNullOrWhiteSpace(this.dataGridView.Rows[this.m_rowIndex].Cells[2].Value.ToString()))
			{
				this.dataGridView.Rows[this.m_rowIndex].Cells[2].Value = this.CompareEnumToText(CompareOperator.Equal);
			}
		}

		private void cmbCompare_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (-1 == this.cmbCompare.SelectedIndex)
			{
				return;
			}
			this.dataGridView.Rows[this.m_rowIndex].Cells[this.m_columnIndex].Value = this.cmbCompare.Text;
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.GetData();
			this.SaveFormDataToXml();
			base.Close();
		}

		private void GetData()
		{
			this.dataGridView.EndEdit();
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				string text = dataGridViewRow.Cells[0].Value.ToString();
				string text2 = dataGridViewRow.Cells[1].Value.ToString();
				string text3 = dataGridViewRow.Cells[2].Value.ToString();
				string value = (dataGridViewRow.Cells[3].Value == null) ? string.Empty : dataGridViewRow.Cells[3].Value.ToString();
				if (!string.IsNullOrWhiteSpace(text2))
				{
					CableListField field = this.GetField(text2);
					if (field != null)
					{
						FilterItem filterItem = new FilterItem();
						filterItem.Field = field;
						filterItem.Relation = this.TextToRelationEnum(text);
						filterItem.Compare = this.TextToCompareEnum(text3);
						filterItem.Value = value;
						this.m_FilterItems.Add(filterItem);
					}
				}
			}
		}

		private RelationOperator TextToRelationEnum(string text)
		{
			if (!("与" == text))
			{
				return RelationOperator.Or;
			}
			return RelationOperator.And;
		}

		private CompareOperator TextToCompareEnum(string text)
		{
			if (!("包含" == text))
			{
				return CompareOperator.Equal;
			}
			return CompareOperator.Contain;
		}

		private string RelationEnumToText(RelationOperator value)
		{
			if (value != RelationOperator.And)
			{
				return "或";
			}
			return "与";
		}

		private string CompareEnumToText(CompareOperator value)
		{
			if (value != CompareOperator.Equal)
			{
				return "包含";
			}
			return "等于";
		}

		private int FindFieldItem(string text)
		{
			foreach (object obj in this.cmbField.Items)
			{
				if (obj.ToString() == text)
				{
					return this.cmbField.Items.IndexOf(obj);
				}
			}
			return -1;
		}

		private const int Item_Count = 6;

		private const string Field_Null = "空";

		private const string XmlNode_Root = "CableListFilter";

		private const string Item_Relation = "Relation";

		private const string Item_Field = "Field";

		private const string Item_Compare = "Compare";

		private const string Item_Value = "Value";

		private int m_rowIndex;

		private int m_columnIndex;

		private List<CableListField> m_FieldList = new List<CableListField>();

		private List<FilterItem> m_FilterItems = new List<FilterItem>();
	}
}
